using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media.Animation;

namespace RSCore.Helper.WPF
{

    /// <summary>
    /// It is a simple "rotating" control used to show that an operation is in progress.
    /// </summary>
    public partial class Spinner : UserControl, IComponentConnector
    {

        #region Variables

        private bool _isRunning;

        #endregion

        #region Constructor

        public Spinner()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Tell if the animation is running.
        /// </summary>
        public bool IsRunning
        {
            get
            {
                return _isRunning;
            }
        }

        #endregion

        #region Start/Stop the animation

        public void Start()
        {
            _isRunning = true;

            spinner.Visibility = Visibility.Visible;

            // Start the animation
            Storyboard storyboard = spinner.FindResource("spin") as Storyboard;
            spinner.BeginAnimation(UIElement.RenderTransformProperty, null);
            storyboard.Begin(spinner, HandoffBehavior.SnapshotAndReplace, true);
        }

        public void Stop()
        {
            _isRunning = false;

            // Hide the control
            spinner.Visibility = Visibility.Hidden;

            // Stop the animation
            (spinner.FindResource("spin") as Storyboard).Stop(spinner);
        }

        #endregion

    }
}

